package com.xulu.romance.controller;

import com.xulu.romance.constants.Constant;
import com.xulu.romance.service.SendMailService;
import com.xulu.romance.service.UserService;
import com.xulu.romance.utils.R;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;

/**
 * @author xiaoxulu
 * @version V1.0
 * @Package com.xulu.romance.controller
 * @date 2024/2/6 16:48
 */
@RestController
@CrossOrigin
@RequestMapping("/api/mail")
@Api(tags = "发送验证码接口")
public class SendMailController {

    @Autowired
    private SendMailService sendMailService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @GetMapping("/front/sendCode/{email}")
    public R sendMail(@PathVariable String email) {
        try {
            if (StringUtils.isNotBlank(email) &&Boolean.TRUE.equals(stringRedisTemplate.opsForSet().isMember("registered_users", email))) {
                return Objects.requireNonNull(new R().put("code", Constant.FAILURE_CODE)).put("message", Constant.USER_EXIST);
            } else if( StringUtils.isNotBlank(email) ){
                sendMailService.sendMailService(email);
            }

        } catch (Exception e) {
            e.printStackTrace();

        }
        return Objects.requireNonNull(R.ok().put("code", Constant.SUCCESS_CODE)).put("message", Constant.SEND_CODE_SUCCESS);


    }

    @GetMapping("/front/sendLoginCode/{email}")
    public R sendLoginCode(@PathVariable String email) {
        try {
            if (StringUtils.isNotBlank(email) &&Boolean.FALSE.equals(stringRedisTemplate.opsForSet().isMember("registered_users", email))) {
                return Objects.requireNonNull(new R().put("code", Constant.FAILURE_CODE)).put("message", Constant.USER_NOT_EXIST);
            } else if( StringUtils.isNotBlank(email) ){
                sendMailService.sendMailService(email);
            }

        } catch (Exception e) {
            e.printStackTrace();

        }
        return Objects.requireNonNull(R.ok().put("code", Constant.SUCCESS_CODE)).put("message", Constant.SEND_CODE_SUCCESS);


    }




}
